Learn how to use R Studio to make a visualization of COVID-19 that will help encourage everyone to do their part to end the coronavirus!

# Use the command echo = TRUE to show the code! This is programmed in R Studio using R Markdown, 
# but I used this command to display the code on this HTML page using echo = TRUE.

knitr::opts_chunk$set(echo = TRUE)

# I need to use several packages and libraries available for download through RStudio 
# in order to make this data visualization.

library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.4
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# This data on COVID-19 comes from the data repository for the 2019 Novel
# Coronavirus Visual Dashboard operated by the Johns Hopkins University
# Center for Systems Science and Engineering (JHU CSSE). Learn more about the data here:
# https://github.com/CSSEGISandData/COVID-19. Load the most up-to-date data
# by changing the date at the end of the file name.

covid <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/04-18-2020.csv")
## Parsed with column specification:
## cols(
##   FIPS = col_double(),
##   Admin2 = col_character(),
##   Province_State = col_character(),
##   Country_Region = col_character(),
##   Last_Update = col_datetime(format = ""),
##   Lat = col_double(),
##   Long_ = col_double(),
##   Confirmed = col_double(),
##   Deaths = col_double(),
##   Recovered = col_double(),
##   Active = col_double(),
##   Combined_Key = col_character()
## )
# I wanted to select the region, latitude, longitude, confirmed cases, and
# number of death variables from the data. I also made a new variable called
# "region" that combines the city, province/state, and country name if
# applicable.

data <- covid %>% 
  select(Admin2, Province_State, Country_Region, Lat, Long_, Confirmed, Deaths) %>%
  mutate(region = case_when(
    is.na(Admin2) & is.na(Province_State) ~ Country_Region,
    is.na(Admin2) & !is.na(Province_State) ~ paste(Province_State, Country_Region, sep = ", "),
    !is.na(Admin2) & !is.na(Province_State) ~ paste(Admin2, Province_State, Country_Region, sep = ", ")
  ))

data
## # A tibble: 3,053 x 8
##    Admin2  Province_State Country_Region   Lat  Long_ Confirmed Deaths region   
##    <chr>   <chr>          <chr>          <dbl>  <dbl>     <dbl>  <dbl> <chr>    
##  1 Abbevi… South Carolina US              34.2  -82.5        15      0 Abbevill…
##  2 Acadia  Louisiana      US              30.3  -92.4       110      7 Acadia, …
##  3 Accoma… Virginia       US              37.8  -75.6        33      0 Accomack…
##  4 Ada     Idaho          US              43.5 -116.        593      9 Ada, Ida…
##  5 Adair   Iowa           US              41.3  -94.5         1      0 Adair, I…
##  6 Adair   Kentucky       US              37.1  -85.3        47      3 Adair, K…
##  7 Adair   Missouri       US              40.2  -92.6        12      0 Adair, M…
##  8 Adair   Oklahoma       US              35.9  -94.7        29      3 Adair, O…
##  9 Adams   Colorado       US              39.9 -104.        860     31 Adams, C…
## 10 Adams   Idaho          US              44.9 -116.          1      0 Adams, I…
## # … with 3,043 more rows
# Go to https://account.mapbox.com/access-tokens/ to get an API access token to
# configure Mapbox GL JS, Mobile, and Mapbox web services like routing and
# geocoding.

Sys.setenv("MAPBOX_TOKEN" = 
          "pk.eyJ1IjoiamVkd2FyZHMyMSIsImEiOiJjazhyamRoaDIwNmo5M2RtenNoNTBlOW9mIn0.RCk9CZZqefyJMNnH76LhaQ")

# Use the plot_mapbox command to initiate a plotly_mapbox object. The size of
# circles on the map correspond to the number of confirmed cases in a region
# with size = ~Confirmed, and the tooltips on the map show the latitude,
# longitude, location name, and number of confirmed cases.

data %>% 
  plot_mapbox(lat = ~Lat, lon = ~Long_,
              size = ~Confirmed,
              text = ~paste(region, "\nConfirmed Cases:", Confirmed),
              mode = "scattermapbox") %>% 
  
  # Use layout to change the design of the map. I changed the title, font,
  # background color, and margins.
  
  layout(title = "Worldwide COVID-19 Confirmed Cases: April 18, 2020",
         font = list(color = "white"),
         plot_bgcolor = "#191A1A", paper_bgcolor = "#191A1A",
         mapbox = list(style = "dark"),
         margin = list(l = 10, r = 10,
                       b = 10, t = 50,
                       pad = 2)) %>% 
  
  # Use the config command to set the Mapbox API access token to the long string
  # of characters defined above.
  
  config(mapboxAccessToken = Sys.getenv("MAPBOX_TOKEN"))